import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_module/config/ServiceUlr.dart';
import 'package:flutter_module/view_model/query_wuliao.dart';
import 'package:flutter_module/widget/provider_widget.dart';
import 'package:flutter_module/widget/tap_appbar.dart';
import 'package:flutter_screenutil/screenutil.dart';

class KuFangWeiHu extends StatefulWidget {
  @override
  _KuFangWeiHu createState() => _KuFangWeiHu();
}

class _KuFangWeiHu extends State<KuFangWeiHu> {
  List<List> list = [];
  var map = {
    "1": "交通维护类",
    "2": "水管接头",
    "3": "水泵控制柜",
    "4": "工作服",
    "5": "安全救生",
    "6": "小工具",
    "7": "排水专用工具",
    "8": "其他",
    "9": "全部",
    "1001": "讯腾物料1",
    "1002": "讯腾物料2"
  };

  var map1 = {
    "materielType": "",
    "materielNo": "",
    "materielName": "",
    "pageIndex": "1",
    "pageSize": "10",
    "companyId": "402880185889ef6e015889f8c51e0000",
    "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
  };

  String value;

  String storehouseNo;
  String storehouseName;
  wuliaoQueryModel queryModel = new wuliaoQueryModel();

  @override
  void initState() {
    // TODO: implement initState
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: TapAppBar(TapViewBar(Text("库房维护"), icon: Icon(Icons.arrow_back)),
          Size.fromHeight(ScreenUtil().setHeight(80))),
      body: ProviderWidget<wuliaoQueryModel>(
        model: wuliaoQueryModel(),
        onModelInit: (model) {
          model.url = "queryMateriels";
          model
              .getHttpData(servicePath["storehouse"], data: map1)
              .then((value) {
            list = model.data;
          });
        },
        builder: (context, model, child) {
          return EasyRefresh(
              controller: model.controller,
              footer: MaterialFooter(),
              enableControlFinishLoad: true,
              onLoad: model.onLoad,
              bottomBouncing: true,
              child: SingleChildScrollView(
                child: Column(
                  children: <Widget>[
                    getInPutName1(),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getInPutName(),
                    SizedBox(
                      height: 1,
                      width: ScreenUtil().setWidth(700),
                      child: Container(
                        color: Colors.black12,
                      ),
                    ),
                    getQuery(model),
                    getWrap(),
                  ],
                ),
              ));
        },
      ),
    );
  }

  List<Widget> getListItem() {
    return list.map((value) {
      return Container(
        margin: EdgeInsets.only(left: 10, right: 10, top: 10),
        padding: EdgeInsets.only(left: 20, top: 10),
        decoration: new BoxDecoration(
          //背景
          color: Colors.white,
          //设置四周圆角 角度
          borderRadius: BorderRadius.all(Radius.circular(4.0)),
          //设置四周边框
//          border: new Border.all(width: 1, color: Colors.red),
        ),
//        color: Colors.white,
        child: Column(
          children: <Widget>[
            getListItemData("序        号:", value[0]),
            SizedBox(
              height: 10,
            ),
            getListItemData("库房名称:", value[1]),
            SizedBox(
              height: 10,
            ),
            getListItemData("库房地址:", value[2]),
            SizedBox(
              height: 10,
            ),

          ],
        ),
      );
    }).toList();
  }

  Widget getListItemData(String name, String data) {
    return Row(
      children: <Widget>[
        Expanded(
          child: Text(
            name,
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          flex: 1,
        ),
        Expanded(
          child: Text(
            data,
            style: TextStyle(
                color: name == "状        态:" ? Colors.red : Colors.black54),
          ),
          flex: 4,
        )
      ],
    );
  }
  Widget getInPutName() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text("库房名称"),
            flex: 1,
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                //  labelText: sp.getUserInfo("num").toString(),
                fillColor: Colors.white,
                filled: true,
                border: InputBorder.none,
                hintText: "",
              ),
              onChanged: (value) {
                storehouseName = value;
              },
            ),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget getInPutName1() {
    return Container(
      color: Colors.white,
      alignment: Alignment.center,
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: Text("        序号"),
            flex: 1,
          ),
          Expanded(
            child: TextField(
              decoration: InputDecoration(
                //  labelText: sp.getUserInfo("num").toString(),
                fillColor: Colors.white,
                filled: true,
                border: InputBorder.none,
                hintText: "",
              ),
              onChanged: (value) {
                storehouseNo = value;
              },
            ),
            flex: 2,
          )
        ],
      ),
    );
  }

  Widget  getQuery(wuliaoQueryModel model) {
    return Container(
      // color: Colors.black12,
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 10),
      height: ScreenUtil().setHeight(80),
      padding: EdgeInsets.only(left: 30, right: 30),
      child: Row(
        children: <Widget>[
          Expanded(
            child: RaisedButton(
              onPressed: () {
//                materielName:
//                materielType:
                var map = {
                  "storehouseNo": this.storehouseNo,
                  "storehouseName": this.storehouseName,
                  "companyId": "402880185889ef6e015889f8c51e0000",
                  "userId": "8a2a2a295c1ec767015c2ddd9d1e0001",
                };
                model.map = map;
                model.query = false;
                model.getHttpData(servicePath["storehouse"], data: map);
//                model
//                    .getHttpData(servicePath["queryMateriels"], data:map1)
//                    .then((value) {
//                  setState(() {
//                    this.list.clear();
//                    model.query = false;
//                    list.addAll(value);
//                  });
//                });
              },
              child: Text("查询"),
              textColor: Colors.white,
              color: Theme.of(context).primaryColor,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(8),
                // side: BorderSide(color: Colors.red)
              ),
            ),
            flex: 1,
          ),
//          Text("   "),
        ],
      ),
    );
  }

  Widget getWrap() {
    if (list == null) {
      return Text(" ");
    } else {
      return Container(
//        padding: EdgeInsets.only(left: 30),
        child: Wrap(
          spacing: 1,
          children: getListItem(),
        ),
      );
    }
  }
}
